package com.example.chamberlainserver.Service.impl;

import com.example.chamberlainserver.Entry.AdjustmentLog;
import com.example.chamberlainserver.Entry.ScheduleAssignment;
import com.example.chamberlainserver.Entry.User;
import com.example.chamberlainserver.Service.AdjustmentLogService;
import com.example.chamberlainserver.Service.ScheduleAssignmentService;
import com.example.chamberlainserver.Service.UserService;
import com.example.chamberlainserver.dto.PageResult;
import com.example.chamberlainserver.mapper.AdjustmentLogMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 调整日志服务实现类
 */
@Service
public class AdjustmentLogServiceImpl implements AdjustmentLogService {

    @Autowired
    private AdjustmentLogMapper adjustmentLogMapper;
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private ScheduleAssignmentService scheduleAssignmentService;

    @Override
    public boolean addAdjustmentLog(AdjustmentLog adjustmentLog) {
        adjustmentLog.setId(UUID.randomUUID().toString());
        adjustmentLog.setCreatedAt(LocalDateTime.now());
        return adjustmentLogMapper.insertAdjustmentLog(adjustmentLog) > 0;
    }

    @Override
    public AdjustmentLog getAdjustmentLogById(String id) {
        AdjustmentLog adjustmentLog = adjustmentLogMapper.getAdjustmentLogById(id);
        if (adjustmentLog != null) {
            loadRelatedData(adjustmentLog);
        }
        return adjustmentLog;
    }

    @Override
    public List<AdjustmentLog> getAdjustmentLogsByAssignmentId(String assignmentId) {
        List<AdjustmentLog> adjustmentLogs = adjustmentLogMapper.getAdjustmentLogsByAssignmentId(assignmentId);
        
        for (AdjustmentLog adjustmentLog : adjustmentLogs) {
            loadRelatedData(adjustmentLog);
        }
        
        return adjustmentLogs;
    }

    @Override
    public List<AdjustmentLog> getAdjustmentLogsByType(String adjustmentType) {
        List<AdjustmentLog> adjustmentLogs = adjustmentLogMapper.getAdjustmentLogsByType(adjustmentType);
        
        for (AdjustmentLog adjustmentLog : adjustmentLogs) {
            loadRelatedData(adjustmentLog);
        }
        
        return adjustmentLogs;
    }

    @Override
    public List<AdjustmentLog> getAdjustmentLogsByAdjustedBy(Integer adjustedBy) {
        List<AdjustmentLog> adjustmentLogs = adjustmentLogMapper.getAdjustmentLogsByAdjustedBy(adjustedBy);
        
        for (AdjustmentLog adjustmentLog : adjustmentLogs) {
            loadRelatedData(adjustmentLog);
        }
        
        return adjustmentLogs;
    }

    @Override
    public List<AdjustmentLog> getAllAdjustmentLogs() {
        List<AdjustmentLog> adjustmentLogs = adjustmentLogMapper.getAllAdjustmentLogs();
        
        for (AdjustmentLog adjustmentLog : adjustmentLogs) {
            loadRelatedData(adjustmentLog);
        }
        
        return adjustmentLogs;
    }

    @Override
    public boolean deleteAdjustmentLog(String id) {
        return adjustmentLogMapper.deleteAdjustmentLogById(id) > 0;
    }

    @Override
    @Transactional
    public boolean deleteAdjustmentLogsByAssignmentId(String assignmentId) {
        return adjustmentLogMapper.deleteAdjustmentLogsByAssignmentId(assignmentId) > 0;
    }

    @Override
    public PageResult<AdjustmentLog> getPagedAdjustmentLogList(Map<String, Object> params) {
        // 查询总记录数
        Long total = adjustmentLogMapper.getPagedAdjustmentLogCount(params);
        
        // 查询数据列表
        List<AdjustmentLog> adjustmentLogs = adjustmentLogMapper.getPagedAdjustmentLogList(params);
        
        // 为每个记录加载关联数据
        for (AdjustmentLog adjustmentLog : adjustmentLogs) {
            loadRelatedData(adjustmentLog);
        }
        
        Integer current = (Integer) params.getOrDefault("current", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 10);
        
        return new PageResult<>(current, pageSize, total, adjustmentLogs);
    }

    @Override
    @Transactional
    public boolean logScheduleAdjustment(String assignmentId, String adjustmentType, String oldValue, String newValue, String reason, Integer adjustedBy) {
        AdjustmentLog adjustmentLog = new AdjustmentLog();
        adjustmentLog.setAssignmentId(assignmentId);
        adjustmentLog.setAdjustmentType(adjustmentType);
        adjustmentLog.setOldValue(oldValue);
        adjustmentLog.setNewValue(newValue);
        adjustmentLog.setReason(reason);
        adjustmentLog.setAdjustedBy(adjustedBy);
        
        return addAdjustmentLog(adjustmentLog);
    }

    /**
     * 为调整日志加载关联数据
     */
    private void loadRelatedData(AdjustmentLog adjustmentLog) {
        if (adjustmentLog.getAssignmentId() != null) {
            ScheduleAssignment scheduleAssignment = scheduleAssignmentService.getScheduleAssignmentById(adjustmentLog.getAssignmentId());
            adjustmentLog.setAssignment(scheduleAssignment);
        }
        
        if (adjustmentLog.getAdjustedBy() != null) {
            User adjustedByUser = userService.getUserById(adjustmentLog.getAdjustedBy());
            adjustmentLog.setAdjustedByUser(adjustedByUser);
        }
    }
}